'use strict';
const path = require('path');
const webpack = require('webpack');
const os = require('os');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
// const HtmlWebpackPlugin = require('html-webpack-plugin');
const glob = require('glob');

let plugins = [
    new webpack.optimize.CommonsChunkPlugin({
        name: 'common',
        filename: 'common.js',
        minChunks: 3,
    }),
    new ExtractTextPlugin({
        filename: '[name].css',
        allChunks: true,
    }),
    new webpack.ProvidePlugin({
        $: 'jquery',
        jQuery: 'jquery',
        'window.jQuery': 'jquery'
    }),
    new webpack.HotModuleReplacementPlugin(),
    // new HtmlWebpackPlugin({
    //     template: 'html-withimg-loader!' + path.resolve('src', 'html/index.html'),
    // filename: 'index.html'
    // }),
];

function getEntry() {
    let entry = {};
    let files = glob.sync(__dirname + '/src/**/*.entry.js');
    for (let file of files) {
        let fileName = path.basename(file, '.entry.js');
        // let tmpl = {
        //     favicon: './src/img/favicon.ico', // favicon路径
        //     filename: `./html/${fileName}.html`,
        //     template: `./src/html/${fileName}.html`,
        //     inject: true,
        //     chunks: ['common', fileName],  // 此处是载入提取的公共js，以及html同名js
        //     hash: true,
        //     minify: {	//压缩HTML文件
        //         removeComments: true,	// 移除HTML中的注释
        //         collapseWhitespace: false	// 删除空白符与换行符
        //     }
        // };
        // plugins.push(new HtmlWebpackPlugin(tmpl));
        entry[fileName] = [file];
    }
    return entry;
}

module.exports = {
    context: path.join(__dirname, 'src'),
    entry: getEntry(),
    output: {
        filename: '[name].js',
        path: path.join(__dirname, 'dist'),
        publicPath: ''
    },
    plugins: plugins,
    devServer: {
        contentBase: path.join(__dirname, 'dist'),
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                use: [{
                    loader: 'babel-loader',
                    // exclude: /node_modules/,
                    query: {
                        presets: ['es2015']
                    }
                }],
            },
            {
                test: /\.css$/,
                use: ['style-loader', 'css-loader'],
            },
            {
                test: /\.less$/,
                use: ['style-loader', 'css-loader', 'less-loader'],
            },
            {
                test: /\.html$/,
                use: ['html-loader'],
            },
            {
                test: /\.(eot|svg|ttf|woff|woff2)$/,
                use: ['url-loader'],
            },
            // {
            //     test: /\.(gif|png|jpg|jpeg)$/,
            //     use: ['url-loader'],
            //     query: {
            //         limit: 8192,
            //         name: 'images/[name].[ext]'
            //     }
            // },
            // Loaders for other file types can go here
        ],
    },
    resolve: {
        modules: [path.resolve(__dirname, 'src'), 'node_modules'],
        alias: {

        },
    },
};